package com.uaroadshow.entity.trade;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.uaroadshow.entity.user.User;
import com.uaroadshow.entity.user.UserInfo;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "sales_order")
@JsonIgnoreProperties(value = { "hibernate_lazy_initializer", "handler", "fieldHandler" })
public class Order {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 订单代码 */
    private String code;
    
    /** 课程 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private Product product;
    
    /** 用户 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private User user;
    
    /** 用户信息 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private UserInfo userInfo;
    
    /** 订单金额 */
    private int price;
    
    /** 支付状态 */
    @Enumerated(EnumType.STRING)
    private PaymentStatus paymentStatus;
    
    /** 支付时间 */
    private Date payTime;
    
    /** 创建时间 */
    private Date createTime;
    
    /** 姓名 */
    private String name;
    
    /** 性别 */
    private String gender;
    
    /** 年龄 */
    private Integer age;
    
    /** 短袖尺码 */
    private String clothesSize;
    
    /** 鞋子尺码 */
    private String shoeSize;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Order() {
    }
    
    /**
     * @param product
     * @param user
     * @param userInfo
     */
    public Order(Product product, User user, UserInfo userInfo) {
        this.product = product;
        this.user = user;
        this.userInfo = userInfo;
        this.code = System.currentTimeMillis() + "";        // FIXME
        this.price = product.getPrice();
        this.name = userInfo.getName();
        this.gender = userInfo.getGender();
        this.age = userInfo.getAge();
        this.clothesSize = userInfo.getClothesSize();
        this.shoeSize = userInfo.getShoeSize();
        this.paymentStatus = PaymentStatus.PENDING;
        this.createTime = new Date();
        this.disabled = false;
    }

    /**
     * @return
     */
    public Order paid() {
        this.paymentStatus = PaymentStatus.PAID;
        this.payTime = new Date();
        return this;
    }
    
    /**
     * 
     * @return
     */
    public String getOrderTime() {
        return DateFormatUtils.format(this.createTime, "yyyy年MM月dd日");
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @return the userInfo
     */
    public UserInfo getUserInfo() {
        return userInfo;
    }

    /**
     * @return the price
     */
    public int getPrice() {
        return price;
    }

    /**
     * @return the paymentStatus
     */
    public PaymentStatus getPaymentStatus() {
        return paymentStatus;
    }

    /**
     * @return the payTime
     */
    public Date getPayTime() {
        return payTime;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @return the age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * @return the clothesSize
     */
    public String getClothesSize() {
        return clothesSize;
    }

    /**
     * @return the shoeSize
     */
    public String getShoeSize() {
        return shoeSize;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @param user the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @param userInfo the userInfo to set
     */
    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * @param paymentStatus the paymentStatus to set
     */
    public void setPaymentStatus(PaymentStatus paymentStatus) {
        this.paymentStatus = paymentStatus;
    }

    /**
     * @param payTime the payTime to set
     */
    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * @param clothesSize the clothesSize to set
     */
    public void setClothesSize(String clothesSize) {
        this.clothesSize = clothesSize;
    }

    /**
     * @param shoeSize the shoeSize to set
     */
    public void setShoeSize(String shoeSize) {
        this.shoeSize = shoeSize;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}